package com.pansky.crm.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pansky.crm.annotation.OperateLog;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.entity.Privilege;
import com.pansky.crm.entity.bo.RolePrivilegeDTO;
import com.pansky.crm.entity.vo.RolePrivilegeTreeVO;
import com.pansky.crm.entity.vo.RolePrivilegeVo;
import com.pansky.crm.enums.PrivilegeTypeEnum;
import com.pansky.crm.service.PrivilegeService;
import com.pansky.crm.service.RolePrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/2/25 11:27
 * @description：角色权限controller
 */
@RestController
@Slf4j
@RequestMapping("/privilege")
@Api(tags = {"管理端-角色权限api"})
@OperateLog
public class RolePrivilegeController {

    @Autowired
    private RolePrivilegeService rolePrivilegeService;
    @Autowired
    private PrivilegeService privilegeService;

    @ApiOperation(value = "获取角色可选的功能权限", notes = "获取角色可选的功能权限")
    @GetMapping("/listPrivilegeByRoleId")
    public ApiResult<RolePrivilegeTreeVO> findMenu(@RequestParam("roleId") Integer roleId) {
        return ApiResult.ok(privilegeService.findMenuPrivilegeByRoleId(roleId));
    }

    @GetMapping("/getAllUrl")
    @ApiOperation(value = "获取所有请求路径", notes = "获取所有请求路径")
    public ApiResult<List<Privilege>> getAllUrl() {
        return ApiResult.ok(privilegeService.list());
    }

    @ApiOperation(value = "查询所有菜单项")
    @PostMapping("/menu/queryAll")
    public ApiResult<List<RolePrivilegeVo>> queryAll() {
        return ApiResult.ok(privilegeService.findMenuPrivilege());
    }

    @ApiOperation(value = "查询菜单功能点", notes = "查询菜单功能点")
    @PostMapping("/function/query")
    public ApiResult<List<Privilege>> functionQuery(@RequestParam String menuKey) {
        QueryWrapper<Privilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_key", menuKey);
        queryWrapper.eq("type", PrivilegeTypeEnum.POINTS.getValue());
        return ApiResult.ok(privilegeService.list(queryWrapper));
    }

    @ApiOperation(value = "更新角色权限", notes = "更新角色权限")
    @PostMapping("/updateRolePrivilege")
    @Transactional
    public ApiResult<Boolean> updateRolePrivilege(@Valid @RequestBody RolePrivilegeDTO updateDTO) {
        return ApiResult.ok(rolePrivilegeService.updateRolePrivilege(updateDTO));
    }

    @ApiOperation(value = "新增菜单权限", notes = "新增菜单权限")
    @PostMapping("/menu/add")
    @Transactional
    public ApiResult<Boolean> addMenuPrivilege(@RequestBody Privilege privilege) {
        return ApiResult.ok(privilegeService.save(privilege));
    }

    @ApiOperation(value = "更新菜单权限", notes = "更新菜单权限")
    @PostMapping("/menu/update")
    @Transactional
    public ApiResult<Boolean> updateMenuPrivilege(@RequestBody Privilege privilege) {
        return ApiResult.ok(privilegeService.updateById(privilege));
    }

    @ApiOperation(value = "删除菜单权限", notes = "删除菜单权限")
    @PostMapping("/menu/delete")
    @Transactional
    public ApiResult<Boolean> deleteMenuPrivilege(@RequestParam Integer privilegeId) {
        return privilegeService.removeByPrivilegeId(privilegeId);
    }
}
